import sys

import pygame

"""
model =  {"name":"","callable":[]}
"""
events = []


def register_event(name: str, _callable: callable):
    global events
    result = list(filter(lambda item: item['name'] == name, events))
    if len(result) > 0:
        result = result[0]
        result['callable'] += [_callable]
    else:
        events += [{"name": name, "callable": [_callable]}]


def remove_event(name: str, _callable: callable):
    global events
    result = list(filter(lambda item: item['name'] == name, events))
    if len(result) > 0:
        result = result[0]
        for __callable in result['callable']:
            if _callable == __callable:
                result['callable'].remove(__callable)
        if len(result['callable']) <= 0:
            events.remove(result)


def test_event(event):
    if event.type == pygame.KEYDOWN:
        print(event.key)
    if event.type == pygame.MOUSEMOTION:
        print(event)
    ...


if __name__ == '__main__':
    pygame.init()
    window = pygame.display.set_mode([600, 600])
    # 添加事件
    register_event("test event 1", test_event)
    # 添加事件
    register_event("test event 2", test_event)
    # 删除事件
    remove_event("test event 1", test_event)
    # print(events)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            for _event in events:
                [_e(event) for _e in _event['callable']]
